home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / dwtext.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  22.0 KB  |  971 lines

  1. /* Copyright (C) 1996, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19.  
  20. // $Id: dwtext.cpp,v 1.2 2000/09/19 19:00:10 lpd Exp $
  21.  
  22.  
  23. // Microsoft Windows 3.n text window for Ghostscript.
  24.  
  25. #include <stdlib.h>
  26. #include <string.h>     // use only far items
  27. #include <ctype.h>
  28.  
  29. #define STRICT
  30. #include <windows.h>
  31. #include <windowsx.h>
  32. #include <commdlg.h>
  33. #include <shellapi.h>
  34.  
  35. #include "dwtext.h"
  36.  
  37. #if defined(_MSC_VER) && defined(__WIN32__)
  38. #define _export
  39. #else
  40.   /* Define  min and max, but make sure to use the identical definition */
  41.   /* to the one that all the compilers seem to have.... */
  42. #  ifndef min
  43. #    define min(a, b) (((a) < (b)) ? (a) : (b))
  44. #  endif
  45. #  ifndef max
  46. #    define max(a, b) (((a) > (b)) ? (a) : (b))
  47. #  endif
  48. #endif
  49.  
  50. #ifndef EOF
  51. #define EOF (-1)
  52. #endif
  53.  
  54. /* sysmenu */
  55. #define M_COPY_CLIP 1
  56.  
  57. LRESULT CALLBACK _export WndTextProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  58. static const char* TextWinClassName = "rjlTextWinClass";
  59. static const POINT TextWinMinSize = {16, 4};
  60.  
  61. void
  62. TextWindow::error(char *message)
  63. {
  64.     MessageBox((HWND)NULL,message,(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  65. }
  66.  
  67. /* Bring Cursor into text window */
  68. void
  69. TextWindow::to_cursor(void)
  70. {
  71. int nXinc=0;
  72. int nYinc=0;
  73. int cxCursor;
  74. int cyCursor;
  75.     cyCursor = CursorPos.y * CharSize.y;
  76.     if ( (cyCursor + CharSize.y > ScrollPos.y + ClientSize.y) 
  77. //      || (cyCursor < ScrollPos.y) ) {
  78. // change to scroll to end of window instead of just making visible
  79. // so that ALL of error message can be seen
  80.       || (cyCursor < ScrollPos.y+ClientSize.y) ) {
  81.         nYinc = max(0, cyCursor + CharSize.y - ClientSize.y) - ScrollPos.y;
  82.         nYinc = min(nYinc, ScrollMax.y - ScrollPos.y);
  83.     }
  84.     cxCursor = CursorPos.x * CharSize.x;
  85.     if ( (cxCursor + CharSize.x > ScrollPos.x + ClientSize.x)
  86.       || (cxCursor < ScrollPos.x) ) {
  87.         nXinc = max(0, cxCursor + CharSize.x - ClientSize.x/2) - ScrollPos.x;
  88.         nXinc = min(nXinc, ScrollMax.x - ScrollPos.x);
  89.     }
  90.     if (nYinc || nXinc) {
  91.         ScrollPos.y += nYinc;
  92.         ScrollPos.x += nXinc;
  93.         ScrollWindow(hwnd,-nXinc,-nYinc,NULL,NULL);
  94.         SetScrollPos(hwnd,SB_VERT,ScrollPos.y,TRUE);
  95.         SetScrollPos(hwnd,SB_HORZ,ScrollPos.x,TRUE);
  96.         UpdateWindow(hwnd);
  97.     }
  98. }
  99.  
  100. void
  101. TextWindow::new_line(void)
  102. {
  103.     CursorPos.x = 0;
  104.     CursorPos.y++;
  105.     if (CursorPos.y >= ScreenSize.y) {
  106.         int i =  ScreenSize.x * (ScreenSize.y - 1);
  107.         _fmemmove(ScreenBuffer, ScreenBuffer+ScreenSize.x, i);
  108.         _fmemset(ScreenBuffer + i, ' ', ScreenSize.x);
  109.         CursorPos.y--;
  110.         ScrollWindow(hwnd,0,-CharSize.y,NULL,NULL);
  111.         UpdateWindow(hwnd);
  112.     }
  113.     if (CursorFlag)
  114.         to_cursor();
  115. //    TextMessage();
  116. }
  117.  
  118. /* Update count characters in window at cursor position */
  119. /* Updates cursor position */
  120. void
  121. TextWindow::update_text(int count)
  122. {
  123. HDC hdc;
  124. int xpos, ypos;
  125.     xpos = CursorPos.x*CharSize.x - ScrollPos.x;
  126.     ypos = CursorPos.y*CharSize.y - ScrollPos.y;
  127.     hdc = GetDC(hwnd);
  128.     SelectFont(hdc, hfont);
  129.     TextOut(hdc,xpos,ypos,
  130.         (LPSTR)(ScreenBuffer + CursorPos.y*ScreenSize.x + CursorPos.x),
  131.         count);
  132.     (void)ReleaseDC(hwnd,hdc);
  133.     CursorPos.x += count;
  134.     if (CursorPos.x >= ScreenSize.x)
  135.         new_line();
  136. }
  137.  
  138.  
  139. void
  140. TextWindow::size(int width, int height)
  141. {
  142.     ScreenSize.x =  max(width, TextWinMinSize.x);
  143.     ScreenSize.y =  max(height, TextWinMinSize.y);
  144. }
  145.  
  146. void
  147. TextWindow::font(const char *name, int size)
  148. {
  149.     // reject inappropriate arguments
  150.     if (name == NULL)
  151.     return;
  152.     if (size < 4)
  153.     return;
  154.  
  155.     // set new name and size
  156.     fontname = new char[strlen(name)+1];
  157.     if (fontname == NULL)
  158.     return;
  159.     strcpy(fontname, name);
  160.     fontsize = size;
  161.  
  162.     // make a new font
  163.     LOGFONT lf;
  164.     TEXTMETRIC tm;
  165.     LPSTR p;
  166.     HDC hdc;
  167.     
  168.     // if window not open, hwnd == 0 == HWND_DESKTOP
  169.     hdc = GetDC(hwnd);
  170.     _fmemset(&lf, 0, sizeof(LOGFONT));
  171.     _fstrncpy(lf.lfFaceName,fontname,LF_FACESIZE);
  172.     lf.lfHeight = -MulDiv(fontsize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
  173.     lf.lfPitchAndFamily = FIXED_PITCH;
  174.     lf.lfCharSet = DEFAULT_CHARSET;
  175.     if ( (p = _fstrstr(fontname," Italic")) != (LPSTR)NULL ) {
  176.     lf.lfFaceName[ (unsigned int)(p-fontname) ] = '\0';
  177.     lf.lfItalic = TRUE;
  178.     }
  179.     if ( (p = _fstrstr(fontname," Bold")) != (LPSTR)NULL ) {
  180.     lf.lfFaceName[ (unsigned int)(p-fontname) ] = '\0';
  181.     lf.lfWeight = FW_BOLD;
  182.     }
  183.     if (hfont)
  184.     DeleteFont(hfont);
  185.  
  186.     hfont = CreateFontIndirect((LOGFONT FAR *)&lf);
  187.  
  188.     /* get text size */
  189.     SelectFont(hdc, hfont);
  190.     GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
  191.     CharSize.y = tm.tmHeight;
  192.     CharSize.x = tm.tmAveCharWidth;
  193.     CharAscent = tm.tmAscent;
  194.     if (bFocus)
  195.     CreateCaret(hwnd, 0, CharSize.x, 2+CaretHeight);
  196.     ReleaseDC(hwnd, hdc);
  197.  
  198.     // redraw window if necessary
  199.     if (hwnd != HWND_DESKTOP) {
  200.     // INCOMPLETE
  201.     }
  202. }
  203.  
  204.  
  205.  
  206. // Set drag strings
  207. void
  208. TextWindow::drag(const char *pre, const char *post)
  209. {
  210.     // remove old strings
  211.     delete DragPre;
  212.     DragPre = NULL;
  213.     delete DragPost;
  214.     DragPost = NULL;
  215.  
  216.     // add new strings
  217.     DragPre = new char[strlen(pre)+1];
  218.     if (DragPre)
  219.     strcpy(DragPre, pre);
  220.     DragPost = new char[strlen(post)+1];
  221.     if (DragPost)
  222.     strcpy(DragPost, post);
  223. }
  224.  
  225. // Text Window constructor
  226. TextWindow::TextWindow(void)
  227. {
  228.     // make sure everything is null
  229.     memset(this, 0, sizeof(TextWindow));
  230.  
  231.     // set some defaults
  232.     font("Courier New", 10);
  233.     size(80, 24);
  234.     KeyBufSize = 2048;
  235.     CursorFlag = 1;    // scroll to cursor after \n or \r
  236. }
  237.  
  238. // TextWindow destructor
  239. TextWindow::~TextWindow(void)
  240. {
  241.     if (hwnd)
  242.         DestroyWindow(hwnd);
  243.     if (hfont)
  244.     DeleteFont(hfont);
  245.  
  246.     delete KeyBuf;
  247.     delete ScreenBuffer;
  248.     delete DragPre;
  249.     delete DragPost;
  250. }
  251.  
  252. // register the window class
  253. int
  254. TextWindow::register_class(HINSTANCE hinst, HICON hicon)
  255. {
  256.     hInstance = hinst;
  257.     hIcon = hicon;
  258.  
  259.     // register window class
  260.     WNDCLASS wndclass;
  261.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  262.     wndclass.lpfnWndProc = WndTextProc;
  263.     wndclass.cbClsExtra = 0;
  264.     wndclass.cbWndExtra = sizeof(void FAR *);
  265.     wndclass.hInstance = hInstance;
  266.     wndclass.hIcon = hIcon ? hIcon : LoadIcon(NULL, IDI_APPLICATION);
  267.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  268.     wndclass.hbrBackground = GetStockBrush(WHITE_BRUSH);
  269.     wndclass.lpszMenuName = NULL;
  270.     wndclass.lpszClassName = TextWinClassName;
  271.     return RegisterClass(&wndclass);
  272. }
  273.  
  274. // Show the window
  275. int
  276. TextWindow::create(LPSTR app_name, int show_cmd)
  277. {
  278.     HMENU sysmenu;
  279.  
  280.     Title = app_name;
  281.     nCmdShow = show_cmd;
  282.     quitnow = FALSE;
  283.  
  284.     // make sure we have some sensible defaults
  285.     if (KeyBufSize < 256)
  286.     KeyBufSize = 256;
  287.  
  288.     CursorPos.x = CursorPos.y = 0;
  289.     bFocus = FALSE;
  290.     bGetCh = FALSE;
  291.     CaretHeight = 0;
  292.  
  293.  
  294.     // allocate buffers
  295.     KeyBufIn = KeyBufOut = KeyBuf = new BYTE[KeyBufSize];
  296.     if (KeyBuf == NULL) {
  297.     error("Out of memory");
  298.     return 1;
  299.     }
  300.     ScreenBuffer = new BYTE[ScreenSize.x * ScreenSize.y];
  301.     if (ScreenBuffer == NULL) {
  302.     error("Out of memory");
  303.     return 1;
  304.     }
  305.     _fmemset(ScreenBuffer, ' ', ScreenSize.x * ScreenSize.y);
  306.  
  307.     hwnd = CreateWindow(TextWinClassName, Title,
  308.           WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  309.           CW_USEDEFAULT, CW_USEDEFAULT,
  310.           CW_USEDEFAULT, CW_USEDEFAULT,
  311.           NULL, NULL, hInstance, this);
  312.  
  313.     if (hwnd == NULL) {
  314.     MessageBox((HWND)NULL,"Couldn't open text window",(LPSTR)NULL, MB_ICONHAND | MB_SYSTEMMODAL);
  315.     return 1;
  316.     }
  317.  
  318.     ShowWindow(hwnd, nCmdShow);
  319.     sysmenu = GetSystemMenu(hwnd,0);    /* get the sysmenu */
  320.     AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  321.     AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  322.  
  323.     return 0;
  324. }
  325.  
  326. int
  327. TextWindow::destroy(void)
  328. {
  329.     if (hwnd) {
  330.         DestroyWindow(hwnd);
  331.     hwnd = (HWND)NULL;
  332.     }
  333.     return 0;
  334. }
  335.  
  336.  
  337.  
  338. int
  339. TextWindow::putch(int ch)
  340. {
  341. int pos;
  342.     switch(ch) {
  343.     case '\r':
  344.         CursorPos.x = 0;
  345.         if (CursorFlag)
  346.             to_cursor();
  347.         break;
  348.     case '\n':
  349.         new_line();
  350.         break;
  351.     case 7:
  352.         MessageBeep(-1);
  353.         if (CursorFlag)
  354.             to_cursor();
  355.         break;
  356.     case '\t':
  357.         {
  358.             for ( int n = 8 - (CursorPos.x % 8); n>0; n-- )
  359.                 putch(' ');
  360.         }
  361.         break;
  362.     case 0x08:
  363.     case 0x7f:
  364.         CursorPos.x--;
  365.         if (CursorPos.x < 0) {
  366.             CursorPos.x = ScreenSize.x - 1;
  367.             CursorPos.y--;
  368.         }
  369.         if (CursorPos.y < 0)
  370.             CursorPos.y = 0;
  371.         break;
  372.     default:
  373.         pos = CursorPos.y*ScreenSize.x + CursorPos.x;
  374.         ScreenBuffer[pos] = ch;
  375.         update_text(1);
  376.     }
  377.     return ch;
  378. }
  379.  
  380. void 
  381. TextWindow::write_buf(LPSTR str, int cnt)
  382. {
  383. BYTE FAR *p;
  384. int count, limit;
  385.     while (cnt>0) {
  386.     p = ScreenBuffer + CursorPos.y*ScreenSize.x + CursorPos.x;
  387.     limit = ScreenSize.x - CursorPos.x;
  388.     for (count=0; (count < limit) && (cnt>0) && (isprint(*str) || *str=='\t'); count++) {
  389.         if (*str=='\t') {
  390.         for ( int n = 8 - ((CursorPos.x+count) % 8); (count < limit) & (n>0); n--, count++ )
  391.             *p++ = ' ';
  392.         str++;
  393.         count--;
  394.            }
  395.         else {
  396.         *p++ = *str++;
  397.         }
  398.         cnt--;
  399.     }
  400.     if (count>0) {
  401.         update_text(count);
  402.     }
  403.     if (cnt > 0) {
  404.         if (*str=='\n') {
  405.         new_line();
  406.         str++;
  407.         cnt--;
  408.         }
  409.         else if (!isprint(*str) && *str!='\t') {
  410.         putch(*str++);
  411.         cnt--;
  412.         }
  413.     }
  414.     }
  415. }
  416.  
  417. // Put string to window
  418. void
  419. TextWindow::puts(LPSTR str)
  420. {
  421.     write_buf(str, lstrlen(str));
  422. }
  423.  
  424.  
  425. /* TRUE if key hit, FALSE if no key */
  426. inline int
  427. TextWindow::kbhit(void)
  428. {
  429.     return (KeyBufIn != KeyBufOut);
  430. }
  431.  
  432. /* get character from keyboard, no echo */
  433. /* need to add extended codes */
  434. int
  435. TextWindow::getch(void)
  436. {
  437.     int ch;
  438.     to_cursor();
  439.     bGetCh = TRUE;
  440.     if (bFocus) {
  441.     SetCaretPos(CursorPos.x*CharSize.x - ScrollPos.x,
  442.         CursorPos.y*CharSize.y + CharAscent - CaretHeight - ScrollPos.y);
  443.     ShowCaret(hwnd);
  444.     }
  445.  
  446.     MSG msg;
  447.     do {
  448.         if (!quitnow && GetMessage(&msg, (HWND)NULL, 0, 0)) {
  449.         TranslateMessage(&msg);
  450.         DispatchMessage(&msg);
  451.     }
  452.     else
  453.        return EOF;    /* window closed */
  454.     } while (!kbhit());
  455.  
  456.     ch = *KeyBufOut++;
  457.     if (ch=='\r')
  458.     ch = '\n';
  459.     if (KeyBufOut - KeyBuf >= KeyBufSize)
  460.     KeyBufOut = KeyBuf;        // wrap around
  461.     if (bFocus)
  462.     HideCaret(hwnd);
  463.     bGetCh = FALSE;
  464.     return ch;
  465. }
  466.  
  467. // Read line from keyboard
  468. // Return at most 'len' characters
  469. // Does NOT add null terminating character
  470. // This does is NOT the same as fgets
  471. int
  472. TextWindow::read_line(LPSTR line, int len)
  473. {    
  474. LPSTR dest = line;
  475. LPSTR limit = dest + len - 1;  // leave room for '\n'
  476. int ch;
  477.     if (dest > limit)
  478.     return 0;
  479.     while ( (ch = getch()) != '\n' ) {
  480.     switch(ch) {
  481.         case EOF:
  482.         case 26:    // ^Z == EOF
  483.         return 0;
  484.         case '\b':    // ^H
  485.         case 0x7f:  // DEL
  486.         if (dest > line) {
  487.             putch('\b');
  488.             putch(' ');
  489.             putch('\b');
  490.             --dest;
  491.         }
  492.         break;
  493.         case 21:    // ^U
  494.         while (dest > line) {
  495.             putch('\b');
  496.             putch(' ');
  497.             putch('\b');
  498.             --dest;
  499.         }
  500.         break;
  501.         default:
  502.         if (dest == limit)
  503.             MessageBeep(-1);
  504.         else {
  505.             *dest++ = ch;
  506.             putch(ch);
  507.         }
  508.         break;
  509.     }
  510.     }
  511.     *dest++ = ch;
  512.     putch(ch);
  513.     return (dest-line);
  514. }
  515.  
  516. // Read a string from the keyboard, of up to len characters
  517. // (not including trailing NULL)
  518. int
  519. TextWindow::gets(LPSTR line, int len)
  520. {
  521. LPSTR dest = line;
  522. LPSTR limit = dest + len;  // don't leave room for '\0'
  523. int ch;
  524.     do {
  525.     if (dest >= limit)
  526.         break;
  527.     ch = getch();
  528.     switch(ch) {
  529.         case 26:    // ^Z == EOF
  530.         return 0;
  531.         case '\b':    // ^H
  532.         case 0x7f:  // DEL
  533.         if (dest > line) {
  534.             putch('\b');
  535.             putch(' ');
  536.             putch('\b');
  537.             --dest;
  538.         }
  539.         break;
  540.         case 21:    // ^U
  541.         while (dest > line) {
  542.             putch('\b');
  543.             putch(' ');
  544.             putch('\b');
  545.             --dest;
  546.         }
  547.         break;
  548.         default:
  549.         *dest++ = ch;
  550.         putch(ch);
  551.         break;
  552.     }
  553.     } while (ch != '\n');
  554.  
  555.     *dest = '\0';
  556.     return (dest-line);
  557. }
  558.  
  559.  
  560. /* Windows 3.1 drag-drop feature */
  561. void
  562. TextWindow::drag_drop(HDROP hdrop)
  563. {
  564.     int i, cFiles;
  565.     LPSTR p;
  566.     if ( (DragPre==(LPSTR)NULL) || (DragPost==(LPSTR)NULL) )
  567.         return;
  568.     char szFile[256];
  569.  
  570.     cFiles = DragQueryFile(hdrop, (UINT)(-1), (LPSTR)NULL, 0);
  571.     for (i=0; i<cFiles; i++) {
  572.     DragQueryFile(hdrop, i, szFile, 80);
  573.     for (p=DragPre; *p; p++)
  574.         SendMessage(hwnd,WM_CHAR,*p,1L);
  575.     for (p=szFile; *p; p++) {
  576.         if (*p == '\\')
  577.         SendMessage(hwnd,WM_CHAR,'/',1L);
  578.         else 
  579.         SendMessage(hwnd,WM_CHAR,*p,1L);
  580.     }
  581.     for (p=DragPost; *p; p++)
  582.         SendMessage(hwnd,WM_CHAR,*p,1L);
  583.     }
  584.     DragFinish(hdrop);
  585. }
  586.  
  587.  
  588. void
  589. TextWindow::copy_to_clipboard(void)
  590. {
  591.     int size, count;
  592.     HGLOBAL hGMem;
  593.     LPSTR cbuf, cp;
  594.     TEXTMETRIC tm;
  595.     UINT type;
  596.     HDC hdc;
  597.     int i;
  598.  
  599.     size = ScreenSize.y * (ScreenSize.x + 2) + 1;
  600.     hGMem = GlobalAlloc(GHND | GMEM_SHARE, (DWORD)size);
  601.     cbuf = cp = (LPSTR)GlobalLock(hGMem);
  602.     if (cp == (LPSTR)NULL)
  603.     return;
  604.     
  605.     for (i=0; i<ScreenSize.y; i++) {
  606.     count = ScreenSize.x;
  607.     _fmemcpy(cp, ScreenBuffer + ScreenSize.x*i, count);
  608.     /* remove trailing spaces */
  609.     for (count=count-1; count>=0; count--) {
  610.         if (cp[count]!=' ')
  611.             break;
  612.         cp[count] = '\0';
  613.     }
  614.     cp[++count] = '\r';
  615.     cp[++count] = '\n';
  616.     cp[++count] = '\0';
  617.     cp += count;
  618.     }
  619.     size = _fstrlen(cbuf) + 1;
  620.     GlobalUnlock(hGMem);
  621.     hGMem = GlobalReAlloc(hGMem, (DWORD)size, GHND | GMEM_SHARE);
  622.     /* find out what type to put into clipboard */
  623.     hdc = GetDC(hwnd);
  624.     SelectFont(hdc, hfont);
  625.     GetTextMetrics(hdc,(TEXTMETRIC FAR *)&tm);
  626.     if (tm.tmCharSet == OEM_CHARSET)
  627.     type = CF_OEMTEXT;
  628.     else
  629.     type = CF_TEXT;
  630.     ReleaseDC(hwnd, hdc);
  631.     /* give buffer to clipboard */
  632.     OpenClipboard(hwnd);
  633.     EmptyClipboard();
  634.     SetClipboardData(type, hGMem);
  635.     CloseClipboard();
  636. }
  637.  
  638. /* text window */
  639. LRESULT CALLBACK _export
  640. WndTextProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  641. {
  642.     TextWindow *tw;
  643.     if (message == WM_CREATE) {
  644.     // Object is stored in window extra data.
  645.     // Nothing must try to use the object before WM_CREATE
  646.     // initializes it here.
  647.     tw = (TextWindow *)(((CREATESTRUCT FAR *)lParam)->lpCreateParams);
  648.     SetWindowLong(hwnd, 0, (LONG)tw);
  649.     }
  650.  
  651.     // call the object window procedure
  652.     tw = (TextWindow *)GetWindowLong(hwnd, 0);
  653.     return tw->WndProc(hwnd, message, wParam, lParam);
  654. }
  655.  
  656.  
  657. // member window procedure
  658. LRESULT
  659. TextWindow::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  660. {
  661.     HDC hdc;
  662.     PAINTSTRUCT ps;
  663.     RECT rect;
  664.     int nYinc, nXinc;
  665.  
  666.     switch(message) {
  667.     case WM_SYSCOMMAND:
  668.         switch(LOWORD(wParam)) {
  669.         case M_COPY_CLIP:
  670.             copy_to_clipboard();
  671.             return 0;
  672.         }
  673.         break;
  674.     case WM_SETFOCUS: 
  675.         bFocus = TRUE;
  676.         CreateCaret(hwnd, 0, CharSize.x, 2+CaretHeight);
  677.         SetCaretPos(CursorPos.x*CharSize.x - ScrollPos.x,
  678.             CursorPos.y*CharSize.y + CharAscent
  679.              - CaretHeight - ScrollPos.y);
  680.         if (bGetCh)
  681.             ShowCaret(hwnd);
  682.         break;
  683.     case WM_KILLFOCUS: 
  684.         DestroyCaret();
  685.         bFocus = FALSE;
  686.         break;
  687.     case WM_SIZE:
  688.         ClientSize.y = HIWORD(lParam);
  689.         ClientSize.x = LOWORD(lParam);
  690.  
  691.         ScrollMax.y = max(0, CharSize.y*ScreenSize.y - ClientSize.y);
  692.         ScrollPos.y = min(ScrollPos.y, ScrollMax.y);
  693.  
  694.         SetScrollRange(hwnd, SB_VERT, 0, ScrollMax.y, FALSE);
  695.         SetScrollPos(hwnd, SB_VERT, ScrollPos.y, TRUE);
  696.  
  697.         ScrollMax.x = max(0, CharSize.x*ScreenSize.x - ClientSize.x);
  698.         ScrollPos.x = min(ScrollPos.x, ScrollMax.x);
  699.  
  700.         SetScrollRange(hwnd, SB_HORZ, 0, ScrollMax.x, FALSE);
  701.         SetScrollPos(hwnd, SB_HORZ, ScrollPos.x, TRUE);
  702.  
  703.         if (bFocus && bGetCh) {
  704.         SetCaretPos(CursorPos.x*CharSize.x - ScrollPos.x,
  705.                 CursorPos.y*CharSize.y + CharAscent 
  706.                 - CaretHeight - ScrollPos.y);
  707.         ShowCaret(hwnd);
  708.         }
  709.         return(0);
  710.     case WM_VSCROLL:
  711.         switch(LOWORD(wParam)) {
  712.         case SB_TOP:
  713.             nYinc = -ScrollPos.y;
  714.             break;
  715.         case SB_BOTTOM:
  716.             nYinc = ScrollMax.y - ScrollPos.y;
  717.             break;
  718.         case SB_LINEUP:
  719.             nYinc = -CharSize.y;
  720.             break;
  721.         case SB_LINEDOWN:
  722.             nYinc = CharSize.y;
  723.             break;
  724.         case SB_PAGEUP:
  725.             nYinc = min(-1,-ClientSize.y);
  726.             break;
  727.         case SB_PAGEDOWN:
  728.             nYinc = max(1,ClientSize.y);
  729.             break;
  730.         case SB_THUMBPOSITION:
  731. #ifdef __WIN32__
  732.             nYinc = HIWORD(wParam) - ScrollPos.y;
  733. #else
  734.             nYinc = LOWORD(lParam) - ScrollPos.y;
  735. #endif
  736.             break;
  737.         default:
  738.             nYinc = 0;
  739.         }
  740.         if ( (nYinc = max(-ScrollPos.y, 
  741.             min(nYinc, ScrollMax.y - ScrollPos.y)))
  742.             != 0 ) {
  743.             ScrollPos.y += nYinc;
  744.             ScrollWindow(hwnd,0,-nYinc,NULL,NULL);
  745.             SetScrollPos(hwnd,SB_VERT,ScrollPos.y,TRUE);
  746.             UpdateWindow(hwnd);
  747.         }
  748.         return(0);
  749.     case WM_HSCROLL:
  750.         switch(LOWORD(wParam)) {
  751.         case SB_LINEUP:
  752.             nXinc = -CharSize.x;
  753.             break;
  754.         case SB_LINEDOWN:
  755.             nXinc = CharSize.x;
  756.             break;
  757.         case SB_PAGEUP:
  758.             nXinc = min(-1,-ClientSize.x);
  759.             break;
  760.         case SB_PAGEDOWN:
  761.             nXinc = max(1,ClientSize.x);
  762.             break;
  763.         case SB_THUMBPOSITION:
  764. #ifdef __WIN32__
  765.             nXinc = HIWORD(wParam) - ScrollPos.x;
  766. #else
  767.             nXinc = LOWORD(lParam) - ScrollPos.x;
  768. #endif
  769.             break;
  770.         default:
  771.             nXinc = 0;
  772.         }
  773.         if ( (nXinc = max(-ScrollPos.x, 
  774.             min(nXinc, ScrollMax.x - ScrollPos.x)))
  775.             != 0 ) {
  776.             ScrollPos.x += nXinc;
  777.             ScrollWindow(hwnd,-nXinc,0,NULL,NULL);
  778.             SetScrollPos(hwnd,SB_HORZ,ScrollPos.x,TRUE);
  779.             UpdateWindow(hwnd);
  780.         }
  781.         return(0);
  782.     case WM_KEYDOWN:
  783.         if (GetKeyState(VK_SHIFT) < 0) {
  784.           switch(wParam) {
  785.         case VK_HOME:
  786.             SendMessage(hwnd, WM_VSCROLL, SB_TOP, (LPARAM)0);
  787.             break;
  788.         case VK_END:
  789.             SendMessage(hwnd, WM_VSCROLL, SB_BOTTOM, (LPARAM)0);
  790.             break;
  791.         case VK_PRIOR:
  792.             SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, (LPARAM)0);
  793.             break;
  794.         case VK_NEXT:
  795.             SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, (LPARAM)0);
  796.             break;
  797.         case VK_UP:
  798.             SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, (LPARAM)0);
  799.             break;
  800.         case VK_DOWN:
  801.             SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, (LPARAM)0);
  802.             break;
  803.         case VK_LEFT:
  804.             SendMessage(hwnd, WM_HSCROLL, SB_LINEUP, (LPARAM)0);
  805.             break;
  806.         case VK_RIGHT:
  807.             SendMessage(hwnd, WM_HSCROLL, SB_LINEDOWN, (LPARAM)0);
  808.             break;
  809.           }
  810.         }
  811.         else {
  812.             switch(wParam) {
  813.             case VK_HOME:
  814.             case VK_END:
  815.             case VK_PRIOR:
  816.             case VK_NEXT:
  817.             case VK_UP:
  818.             case VK_DOWN:
  819.             case VK_LEFT:
  820.             case VK_RIGHT:
  821.             case VK_DELETE:
  822.             { /* store key in circular buffer */
  823.             long count = KeyBufIn - KeyBufOut;
  824.             if (count < 0) count += KeyBufSize;
  825.             if (count < KeyBufSize-2) {
  826.                 *KeyBufIn++ = 0;
  827.                 if (KeyBufIn - KeyBuf >= KeyBufSize)
  828.                 KeyBufIn = KeyBuf;    /* wrap around */
  829.                 *KeyBufIn++ = HIWORD(lParam) & 0xff;
  830.                 if (KeyBufIn - KeyBuf >= KeyBufSize)
  831.                 KeyBufIn = KeyBuf;    /* wrap around */
  832.             }
  833.             }
  834.             }
  835.         }
  836.         break;
  837.     case WM_CHAR:
  838.         { /* store key in circular buffer */
  839.         long count = KeyBufIn - KeyBufOut;
  840.         if (count < 0) count += KeyBufSize;
  841.         if (count < KeyBufSize-1) {
  842.             *KeyBufIn++ = wParam;
  843.             if (KeyBufIn - KeyBuf >= KeyBufSize)
  844.             KeyBufIn = KeyBuf;    /* wrap around */
  845.         }
  846.         }
  847.         return(0);
  848.     case WM_PAINT:
  849.         {
  850.         POINT source, width, dest;
  851.         hdc = BeginPaint(hwnd, &ps);
  852.         SelectFont(hdc, hfont);
  853.         SetMapMode(hdc, MM_TEXT);
  854.         SetBkMode(hdc,OPAQUE);
  855.         GetClientRect(hwnd, &rect);
  856.         source.x = (rect.left + ScrollPos.x) / CharSize.x;        /* source */
  857.         source.y = (rect.top + ScrollPos.y) / CharSize.y;
  858.         dest.x = source.x * CharSize.x - ScrollPos.x;                 /* destination */
  859.         dest.y = source.y * CharSize.y - ScrollPos.y;
  860.         width.x = ((rect.right  + ScrollPos.x + CharSize.x - 1) / CharSize.x) - source.x; /* width */
  861.         width.y = ((rect.bottom + ScrollPos.y + CharSize.y - 1) / CharSize.y) - source.y;
  862.         if (source.x < 0)
  863.             source.x = 0;
  864.         if (source.y < 0)
  865.             source.y = 0;
  866.         if (source.x+width.x > ScreenSize.x)
  867.             width.x = ScreenSize.x - source.x;
  868.         if (source.y+width.y > ScreenSize.y)
  869.             width.y = ScreenSize.y - source.y;
  870.         /* for each line */
  871.         while (width.y>0) {
  872.             TextOut(hdc,dest.x,dest.y,
  873.             (LPSTR)(ScreenBuffer + source.y*ScreenSize.x + source.x),
  874.             width.x);
  875.             dest.y += CharSize.y;
  876.             source.y++;
  877.             width.y--;
  878.         }
  879.         EndPaint(hwnd, &ps);
  880.         return 0;
  881.         }
  882.     case WM_DROPFILES:
  883.         drag_drop((HDROP)wParam);
  884.         break;
  885.     case WM_CREATE:
  886.         {
  887.         RECT crect, wrect;
  888.  
  889.         TextWindow::hwnd = hwnd;
  890.  
  891.         GetClientRect(hwnd, &crect);
  892.         if ( (CharSize.y*ScreenSize.y < crect.bottom)
  893.           || (CharSize.x*ScreenSize.x < crect.right) ) {
  894.             /* shrink size */
  895.         GetWindowRect(hwnd,&wrect);
  896.         MoveWindow(hwnd, wrect.left, wrect.top,
  897.              wrect.right-wrect.left + (CharSize.x*ScreenSize.x - crect.right),
  898.              wrect.bottom-wrect.top + (CharSize.y*ScreenSize.y - crect.bottom),
  899.              TRUE);
  900.         }
  901.         if ( (DragPre!=(LPSTR)NULL) && (DragPost!=(LPSTR)NULL) )
  902.         DragAcceptFiles(hwnd, TRUE);
  903.         }
  904.         break;
  905.     case WM_CLOSE:
  906.         break;
  907.     case WM_DESTROY:
  908.         DragAcceptFiles(hwnd, FALSE);
  909.         if (hfont)
  910.         DeleteFont(hfont);
  911.         hfont = (HFONT)0;
  912.         quitnow = TRUE;
  913.         PostQuitMessage(0);
  914.         break;
  915.     }
  916.     return DefWindowProc(hwnd, message, wParam, lParam);
  917. }
  918.  
  919.  
  920.  
  921.  
  922. #ifdef NOTUSED
  923. // test program
  924. #pragma argsused
  925.  
  926. int PASCAL 
  927. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  928. {
  929.  
  930. // make a test window
  931. TextWindow* textwin = new TextWindow(hInstance);
  932.     if (!hPrevInstance) {
  933.         HICON hicon = LoadIcon(NULL, IDI_APPLICATION);
  934.         textwin->register_class(hicon);
  935.     }
  936.     textwin->font("Courier New", 10);
  937.     textwin->size(80, 80);
  938.     textwin->drag("(", ") run\r");
  939.  
  940.     // show the text window
  941.     if (!textwin->show("Application Name", nCmdShow)) {
  942.         // do the real work here
  943.         // TESTING
  944.         int ch;
  945.         int len;
  946.         char *line = new char[256];
  947.         while ( (len = textwin->read_line(line, 256-1)) != 0 ) {
  948.         textwin->write_buf(line, len);
  949.         }
  950. /*
  951.         while ( textwin->gets(line, 256-1) ) {
  952.         textwin->puts(line);
  953.         }
  954. */
  955. /*
  956.         while ( (ch = textwin->getch()) != 4 )
  957.         textwin->putch(ch);
  958. */
  959.     }
  960.     else {
  961.         
  962.     }
  963.  
  964.     // clean up
  965.     delete textwin;
  966.     
  967.     // end program
  968.     return 0;
  969. }
  970. #endif
  971.